<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">
    <title>Vue.js计算器</title>
    <base href="/">
    <script src="/static/js/toolsbox/app_root.js"></script>
    <script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script>
    <style>
        .demo {
            width: 250px;
            margin: 10px auto;
            padding: 6px;
            border: 2px solid gray;
            border-radius: 4px;
            background: rgb(230, 230, 230);
            box-shadow: 5px 5px 3px aquamarine;
            user-select:none;
            position:absolute;
            top: calc(50% - 225px);
            left: calc(50% - 125px);
        }

        .title {
            font-size: 12px;
            text-align: right;
            font-weight: bold;
            color: gray;
        }

        .display {
            box-sizing: border-box;
            border-radius: 4px;
        }

        .exp {
            height: 60px;
            width: 100%;
            background: rgb(230, 230, 230);
            font-size: 30px;
            line-height: 60px;
            margin: 0;
            box-sizing: border-box;
            border: none;

        }

        .result {
            height: 30px;
            width: 100%;
            margin: 0;
            background: rgb(230, 230, 230);
            text-align: right;
            box-sizing: border-box;
            border: none;
        }

        .btns {
            width: 100%;
        }

        .cal_btn {
            float: left;
            height: 60px;
            width: calc(25% - 4px);
            margin: 2px;
            text-align: center;
            line-height: 60px;
            font-size: 18px;
            background: rgb(250, 250, 250);
            border-radius: 4px;
            cursor: pointer;
        }

        .cal_btn:hover {
            background: rgb(209, 209, 209);
        }
        .tips{
            color:gray;
            font-size:10px;
            text-align: center;
        }
        .clearfix:after {
            content: " ";
            display: block;
            clear: both;
            height: 0;
        }

        .clearfix {
            zoom: 1;
        }
    </style>
</head>
<body>
<div class="demo clearfix" id="demo">
    <div class="title">Vue计算器</div>
    <div class="display">
        <input type="text" class="exp" v-model="string_exp" id="exp" disabled>
        <hr>
        <input type="text" class="result" v-model="result_com" disabled>
    </div>
    <hr>
    <div class="btns clearfix">
        <div class="cal_btn" @click="str_cat($event)">1</div>
        <div class="cal_btn" @click="str_cat($event)">2</div>
        <div class="cal_btn" @click="str_cat($event)">3</div>
        <div class="cal_btn" @click="str_cat($event)">+</div>
        <div class="cal_btn" @click="str_cat($event)">4</div>
        <div class="cal_btn" @click="str_cat($event)">5</div>
        <div class="cal_btn" @click="str_cat($event)">6</div>
        <div class="cal_btn" @click="str_cat($event)">-</div>
        <div class="cal_btn" @click="str_cat($event)">7</div>
        <div class="cal_btn" @click="str_cat($event)">8</div>
        <div class="cal_btn" @click="str_cat($event)">9</div>
        <div class="cal_btn" @click="str_cat($event)">x</div>
        <div class="cal_btn" @click="str_cat($event)">C</div>
        <div class="cal_btn" @click="str_cat($event)">0</div>
        <div class="cal_btn" @click="str_cat($event)">.</div>
        <div class="cal_btn" @click="str_cat($event)">÷</div>
    </div>
    <div class="tips">加 +、减 -、乘 x、除 /、退格 backspace</div>
</div>
<script>
    var vm = new Vue({
        el: "#demo",
        data: {
            string_exp: '0'
        },
        methods: {
            str_cat(event) {
                var element = event.target.innerHTML;
                var the_last = this.string_exp.charAt(this.string_exp.length - 1);
                // 置换标准运算符
                if (element === "÷") {
                    element = "/";
                } else if (element === "x") {
                    element = "*";
                }
                // 如果上一次输入是运算符，则替换
                if ((element === "+" || element === "-" || element === "*" || element === "/" || element === ".") && (the_last === "+" || the_last === "-" || the_last === "*" || the_last === "/" || the_last === ".")) {
                    var old = this.string_exp;
                    this.string_exp = old.slice(0, -1) + element;
                    return;
                }
                if (element === "C") {
                    element = "0";
                    this.string_exp = "0";
                }
                // 计算式追加，如果只有0，且输入不是小数点，则替换
                if (this.string_exp === "0" && element !== ".") {
                    this.string_exp = element;
                } else {
                    // 如果计算式不只有0，或输入是小数点，则追加
                    this.string_exp += element;
                }
            },
            str_back(event){
                if(event.keyCode===8){
                    if (this.string_exp!=="0" && this.string_exp.length>1){
                        this.string_exp = this.string_exp.slice(0, -1);
                    }else{
                        this.string_exp = "0";
                    }
                }
            },
            key_str_cat(event){
                var element_key = event.keyCode;
                var keyCode_legal = [48,49,50,51,52,53,54,55,56,57, 187,189,190,191,88];
                var key_element = {48:"0",49:"1",50:"2",51:"3",52:"4",53:"5",54:"6",55:"7",56:"8",57:"9",187:"+",189:"-",190:".",191:"÷",88:"x"}
                if (!keyCode_legal.includes(element_key)){
                    return;
                }
                var element = key_element[element_key];
                var the_last = this.string_exp.charAt(this.string_exp.length - 1);
                // 置换标准运算符
                if (element === "÷") {
                    element = "/";
                } else if (element === "x") {
                    element = "*";
                }
                // 如果上一次输入是运算符，则替换
                if ((element === "+" || element === "-" || element === "*" || element === "/" || element === ".") && (the_last === "+" || the_last === "-" || the_last === "*" || the_last === "/" || the_last === ".")) {
                    var old = this.string_exp;
                    this.string_exp = old.slice(0, -1) + element;
                    return;
                }
                if (element === "C") {
                    element = "0";
                    this.string_exp = "0";
                }
                // 计算式追加，如果只有0，且输入不是小数点，则替换
                if (this.string_exp === "0" && element !== ".") {
                    this.string_exp = element;
                } else {
                    // 如果计算式不只有0，或输入是小数点，则追加
                    this.string_exp += element;
                }

            },
        },
        computed: {
            result_com() {
                try {
                    return eval(this.string_exp);
                } catch (e) {
                    try {
                        return eval(this.string_exp.slice(0, -1))
                    } catch {
                        return "ERROR";
                    }
                }
            },
        },
        mounted:function() {
            window.addEventListener('keydown',this.str_back);
            window.addEventListener('keydown',this.key_str_cat);
        },
    })

    setInterval(function(){
        var oExp = document.getElementById("exp")
        if(oExp.value.length>17){
            oExp.style.fontSize = "14px";
        }
        else{
            oExp.style.fontSize = "30px";
        }
    }, 100);
</script>
</body>
</html>
